package io.ukoko.controller;

import io.ukoko.dto.Result;
import io.ukoko.entity.Menu;
import io.ukoko.service.MenuService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
public class MenuController {

    @Resource
    private MenuService menuService;


    @GetMapping(value = "/getMenuList")
    public List<Menu> getMenuList(){
        return menuService.getMenuList();
    }

    @PostMapping(value = "/addMenu")
    public Result addMenu(@RequestBody Menu menu){
        menuService.addMenu(menu);
        return Result.ok();
    }

    @GetMapping(value = "/deleteMenu")
    public Result deleteMenu(Integer id){
        List<Menu> menus = menuService.getMenuListById(id);
        if(!CollectionUtils.isEmpty(menus)){
            return Result.error("有子节点,不能删除");
        }else {
            menuService.deleteMenu(id);
            return Result.ok();
        }
    }

    @PostMapping(value = "/updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        menuService.updateMenu(menu);
        return Result.ok();
    }
}
